package com.pai4j.common.util.file;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

/**
 * @Author: 程序员七七
 * @Date: 30.4.22 11:35 下午
 */
@Slf4j
public class ImageUtils {


    private static final int critical = 30;


    /***
     * 处理图片背景色
     * @return
     */
    public static BufferedImage handleBufferImageBackgroundRGB(String path, int targetRgb, boolean isNetWork) throws Exception {
        File file;
        if (isNetWork) {
            file = FileUtil.downloadNetWorkFile(path);
        } else {
            file = new File(path);
        }
        /**
         * 用来处理图片的缓冲流
         */
        BufferedImage bi = null;
        try {
            /**
             * 用ImageIO将图片读入到缓冲中
             */
            bi = ImageIO.read(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /**
         * 定义一个RGB的数组，因为图片的RGB模式是由三个 0-255 来表示的 比如白色就是(255,255,255)
         */
        //                int[] rgb = new int[3];
        //                handleRGB[0] = (nowPixel & 0xff0000) >> 16;
        //                handleRGB[1] = (nowPixel & 0xff00) >> 8;
        //                handleRGB[2] = (nowPixel & 0xff);

        /**
         * 得到图片的长宽
         */
        int width = bi.getWidth();
        int height = bi.getHeight();
        System.out.println("正在处理："+file.getName());

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        /**
         * 获取左上角颜色，默认左上角像素块颜色为背景色
         */
        int pixel = bi.getRGB(critical, critical);
        log.info("图片名称:{}, targetRgb:{}, width:{}, height:{}, pixel:{}",
                file.getName(), targetRgb, width, height, pixel);

        /**
         * 这里是遍历图片的像素，因为要处理图片的背色，所以要把指定像素上的颜色换成目标颜色
         * 这里 是一个二层循环，遍历长和宽上的每个像素
         */
        Graphics g = image.getGraphics();
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                /**
                 * 得到指定像素（i,j)上的RGB值，
                 */
                int nowPixel = bi.getRGB(x, y);
                /**
                 * 进行换色操作，我这里是要把蓝底换成白底，那么就判断图片中rgb值是否在蓝色范围的像素
                 */
                int p;
                if (pixel == nowPixel) {
                    p = targetRgb;
                } else {
                    p = nowPixel;
                }
                if (Math.abs(nowPixel - pixel) < 100000) {
                    p = targetRgb;
                }
                g.setColor(new Color(p));
                g.fillRect(x, y, 1, 1);
            }
        }
        log.info("处理完毕：{}",file.getName());
        return image;
    }

    public static void handleNetWorkImageBackgroundRGB(String path, int targetRgb, boolean isNetWork) throws Exception {
        // 处理背景色
        BufferedImage bi = handleBufferImageBackgroundRGB(path, targetRgb, isNetWork);
        /**
         * 将缓冲对象保存到新文件中
         */
        String outImagePath = "/Users/wangqi49/Desktop/image/"+System.currentTimeMillis()+".jpg";
        File outFile = new File(outImagePath);
        if (!outFile.exists()) {
            outFile.createNewFile();
        }
        FileOutputStream ops = new FileOutputStream(outFile);
        ImageIO.write(bi, "jpg", ops);
        ops.flush();
        ops.close();
    }


    public static void main(String[] args) {
        try {
            handleNetWorkImageBackgroundRGB("/Users/wangqi49/Desktop/image/resume.png", 0xFFFFFF, false);
//            handleNetWorkImageBackgroundRGB("/Users/wangqi49/Desktop/image/resume2.png", 0xFFFFFF);
//            handleNetWorkImageBackgroundRGB("/Users/wangqi49/Desktop/image/resume3.png", 0x007aff);
//            handleNetWorkImageBackgroundRGB("/Users/wangqi49/Desktop/image/resume3.png", 0xe84a5f);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
